package com.qidianit.mes.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.Supplier;
import com.qidianit.mes.sys.mapper.SupplierMapper;
import com.qidianit.mes.sys.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Description:
 * @ Author: DuYaTao
 * @ Date: 2025/9/16 - 20:02
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public Result<PageInfo<Supplier>> getSuppliers(Supplier supplier, Integer pageNum, Integer size) {
        //开启分页
        PageHelper.startPage(pageNum, size);
        //取出供应商数据
        List<Supplier> suppliers = supplierMapper.selectAllSupplier(supplier);
        PageInfo<Supplier> pageInfo = new PageInfo<>(suppliers);
        //最终返回的是一个Result，其中的数据是PageInfo
        return Result.success(pageInfo);
    }

    @Override
    public Result<Supplier> getSupplierByCode(String code) {
        return Result.success(supplierMapper.selectBySupplierCode(code));
    }

    @Override
    public Result addSupplier(Supplier supplier) {
        return supplierMapper.insertSupplier(supplier)==1 ? Result.success(supplier) : Result.error("新增失败");
    }

    @Override
    public Result updateSupplier(Supplier supplier) {
        return supplierMapper.updateByIdSupplier(supplier) == 1 ?Result.success(supplier) : Result.error("修改失败");
    }

    @Override
    public Result deleteSupplier(Long id) {
        return supplierMapper.deleteByIdSupplier(id) == 1 ? Result.success() : Result.error("删除失败");
    }

    @Override
    public Result batchDeleteSupplier(Long[] ids) {
        return supplierMapper.batchSupplier(ids) > 0 ? Result.success() : Result.error("删除失败");
    }
}
